package com.example.suze.libraryinformationmanagementsystem.Activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.suze.libraryinformationmanagementsystem.Adapter.FragmentAdapter;
import com.example.suze.libraryinformationmanagementsystem.Fragment.AdministrationFragment;
import com.example.suze.libraryinformationmanagementsystem.Fragment.ClassificationFragment;
import com.example.suze.libraryinformationmanagementsystem.Fragment.FindFragment;
import com.example.suze.libraryinformationmanagementsystem.R;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements View.OnClickListener{
    private TextView classification1,find1,administration1;//菜单
    private ImageView tablineIv;
    private ViewPager viewPager;
    private AdministrationFragment administrationFragment;
    private ClassificationFragment classificationfragment;
    private FindFragment findFragment;
    private  List<Fragment> list = new ArrayList<Fragment>();

    //ViewPager的当前选中页
    private int currentIndex;
    //屏幕的宽度
    private int screenWidth;
    private FragmentAdapter mFragmentAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findView();
        initTabLineWidth();
        init();
        setClick();

    }
    private void findView(){
        viewPager = (ViewPager) findViewById(R.id.pager);
        classification1 = (TextView) findViewById(R.id.classification);
        find1 = (TextView) findViewById(R.id.find);
        administration1 = (TextView) findViewById(R.id.administration);
        tablineIv = (ImageView) findViewById(R.id.iv_tabline);
    }
    private void init(){
        classificationfragment = new ClassificationFragment();
        findFragment = new FindFragment();
        administrationFragment = new AdministrationFragment();
        list.add(classificationfragment);
        list.add(findFragment);
        list.add(administrationFragment);
        //重写一个FragmentAdapter继承FragmentPagerAdapter，需要传FragmentManager和存放页面的容器过去
        mFragmentAdapter = new FragmentAdapter(this.getSupportFragmentManager(), list);
        //ViewPager绑定监听器
        viewPager.setAdapter(mFragmentAdapter);
        //ViewPager设置默认当前的项
        viewPager.setCurrentItem(0);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            /**
             * state滑动中的状态 有三种状态（0，1，2） 1：正在滑动 2：滑动完毕 0：什么都没做。
             * 三个方法的执行顺序为：用手指拖动翻页时，最先执行一遍onPageScrollStateChanged（1），
             * 然后不断执行onPageScrolled，放手指的时候，直接立即执行一次onPageScrollStateChanged（2），
             * 然后立即执行一次onPageSelected，然后再不断执行onPageScrolled，
             * 最后执行一次onPageScrollStateChanged（0）。
             */

            /**
             * state滑动中的状态 有三种状态（0，1，2） 1：正在滑动 2：滑动完毕 0：什么都没做。
             */
            @Override
            public void onPageScrollStateChanged(int state) {
//                Log.i("PageScroll：", "onPageScrollStateChanged" + ":" + state);
            }

            /**
             * position :当前页面，及你点击滑动的页面 offset:当前页面偏移的百分比
             * offsetPixels:当前页面偏移的像素位置
             */
            @Override
            public void onPageScrolled(int position, float offset,
                                       int offsetPixels) {
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tablineIv.getLayoutParams();
                Log.i("mOffset", "offset:" + offset + ",position:" + position);
                /**
                 * 利用currentIndex(当前所在页面)和position(下一个页面)以及offset来
                 * 设置mTabLineIv的左边距 滑动场景：
                 * 记3个页面,
                 * 从左到右分别为0,1,2
                 * 0->1; 1->2; 2->1; 1->0
                 */
                if (currentIndex == 0 && position == 0)// 0->1
                {
                    lp.leftMargin = (int) (offset * (screenWidth * 1.0 / 3) + currentIndex
                            * (screenWidth / 3));

                } else if (currentIndex == 1 && position == 0) // 1->0
                {
                    lp.leftMargin = (int) (-(1 - offset)
                            * (screenWidth * 1.0 / 3) + currentIndex
                            * (screenWidth / 3));

                } else if (currentIndex == 1 && position == 1) // 1->2
                {
                    lp.leftMargin = (int) (offset * (screenWidth * 1.0 / 3) + currentIndex
                            * (screenWidth / 3));
                } else if (currentIndex == 2 && position == 1) // 2->1
                {
                    lp.leftMargin = (int) (-(1 - offset)
                            * (screenWidth * 1.0 / 3) + currentIndex
                            * (screenWidth / 3));
                }
                tablineIv.setLayoutParams(lp);
            }

            /**
             * 将当前选择的页面的标题设置字体颜色为蓝色
             */
            @Override
            public void onPageSelected(int position) {
                Log.i("PageScroll：", "onPageSelected" + ":" + position);
                resetTextView();
                switch (position) {
                    case 0:
                        classification1.setTextColor(Color.BLUE);
                        break;
                    case 1:
                        find1.setTextColor(Color.BLUE);
                        break;
                    case 2:
                        administration1.setTextColor(Color.BLUE);
                        break;
                }
                currentIndex = position;
            }
        });

    }

    /**
     * 设置滑动条的宽度为屏幕的1/3(根据Tab的个数而定)
     */
    private void initTabLineWidth() {
        DisplayMetrics dpMetrics = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(dpMetrics);
        screenWidth = dpMetrics.widthPixels;
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tablineIv.getLayoutParams();
        lp.width = screenWidth / 3;
        tablineIv.setLayoutParams(lp);
    }

    /**
     * 重置颜色
     */
    private void resetTextView() {
        classification1.setTextColor(Color.BLACK);
        find1.setTextColor(Color.BLACK);
        administration1.setTextColor(Color.BLACK);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.classification:
                viewPager.setCurrentItem(0);
                break;
            case R.id.find:
                viewPager.setCurrentItem(1);
                break;
            case R.id.administration:
                viewPager.setCurrentItem(2);
                break;
        }
    }

    /**
     * 设置点击事件
     */
    private void  setClick(){
        classification1.setOnClickListener(this);
        find1.setOnClickListener(this);

        administration1.setOnClickListener(this);
    }
}


